home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_qt.idb / usr / freeware / include / Qt / qstring.h.z / qstring.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  10.7 KB  |  374 lines

  1. /****************************************************************************
  2. ** $Id: qstring.h,v 2.10 1998/07/03 00:09:47 hanord Exp $
  3. **
  4. ** Definition of extended char array operations, and QByteArray and
  5. ** QString classes
  6. **
  7. ** Created : 920609
  8. **
  9. ** Copyright (C) 1992-1998 Troll Tech AS.  All rights reserved.
  10. **
  11. ** This file is part of Qt Free Edition, version 1.40.
  12. **
  13. ** See the file LICENSE included in the distribution for the usage
  14. ** and distribution terms, or http://www.troll.no/free-license.html.
  15. **
  16. ** IMPORTANT NOTE: You may NOT copy this file or any part of it into
  17. ** your own programs or libraries.
  18. **
  19. ** Please see http://www.troll.no/pricing.html for information about 
  20. ** Qt Professional Edition, which is this same library but with a
  21. ** license which allows creation of commercial/proprietary software.
  22. **
  23. *****************************************************************************/
  24.  
  25. #ifndef QSTRING_H
  26. #define QSTRING_H
  27.  
  28. #ifndef QT_H
  29. #include "qarray.h"
  30. #endif // QT_H
  31.  
  32. #include <string.h>
  33.  
  34. #if defined(_OS_SUN_) && defined(_CC_GNU_)
  35. #include <strings.h>
  36. #endif
  37.  
  38.  
  39. /*****************************************************************************
  40.   Fixes and workarounds for some platforms
  41.  *****************************************************************************/
  42.  
  43. #if defined(_OS_HPUX_)
  44. // HP-UX has badly defined strstr() etc.
  45. inline char *hack_strstr( const char *s1, const char *s2 )
  46. { return (char *)strstr(s1, s2); }
  47. inline char *hack_strchr( const char *s, int c )
  48. { return (char *)strchr(s, c); }
  49. inline char *hack_strrchr( const char *s, int c )
  50. { return (char *)strrchr(s, c); }
  51. #define strstr    hack_strstr
  52. #define strchr    hack_strchr
  53. #define strrchr hack_strrchr
  54. #endif
  55.  
  56.  
  57. /*****************************************************************************
  58.   Safe and portable C string functions; extensions to standard string.h
  59.  *****************************************************************************/
  60.  
  61. void *qmemmove( void *dst, const void *src, uint len );
  62.  
  63. #if defined(_OS_SUN_) || defined(_CC_OC_)
  64. #define memmove qmemmove
  65. #endif
  66.  
  67. char *qstrdup( const char * );
  68.  
  69. inline uint cstrlen( const char *str )
  70. { return strlen(str); }
  71.  
  72. inline uint qstrlen( const char *str )
  73. { return str ? strlen(str) : 0; }
  74.  
  75. #undef    strlen
  76. #define strlen qstrlen
  77.  
  78. inline char *cstrcpy( char *dst, const char *src )
  79. { return strcpy(dst,src); }
  80.  
  81. inline char *qstrcpy( char *dst, const char *src )
  82. { return src ? strcpy(dst, src) : 0; }
  83.  
  84. #undef    strcpy
  85. #define strcpy qstrcpy
  86.  
  87. char *qstrncpy( char *dst, const char *src, uint len );
  88.  
  89. inline int cstrcmp( const char *str1, const char *str2 )
  90. { return strcmp(str1,str2); }
  91.  
  92. inline int qstrcmp( const char *str1, const char *str2 )
  93. { return (str1 && str2) ? strcmp(str1,str2) : (int)((long)str2 - (long)str1); }
  94.  
  95. #undef    strcmp
  96. #define strcmp qstrcmp
  97.  
  98. inline int cstrncmp( const char *str1, const char *str2, uint len )
  99. { return strncmp(str1,str2,len); }
  100.  
  101. inline int qstrncmp( const char *str1, const char *str2, uint len )
  102. { return (str1 && str2) ? strncmp(str1,str2,len) :
  103.               (int)((long)str2 - (long)str1); }
  104.  
  105. #undef    strncmp
  106. #define strncmp qstrncmp
  107.  
  108. int qstricmp( const char *, const char * );
  109. int qstrnicmp( const char *, const char *, uint len );
  110.  
  111. #undef    stricmp
  112. #define stricmp     qstricmp
  113. #undef    strnicmp
  114. #define strnicmp qstrnicmp
  115.  
  116.  
  117. // qchecksum: Internet checksum
  118.  
  119. #if 1    /* OBSOLETE */
  120. #if !defined(QT_CLEAN_NAMESPACE)
  121. UINT16 qchecksum( const char *s, uint len );
  122. #endif
  123. #endif
  124. Q_UINT16 qChecksum( const char *s, uint len );
  125.  
  126. /*****************************************************************************
  127.   QByteArray class
  128.  *****************************************************************************/
  129.  
  130. #if defined(USE_TEMPLATECLASS)
  131. #define QByteArray QArrayT<char>
  132. #else
  133. Q_DECLARE(QArrayM,char);
  134. #define QByteArray QArrayM(char)
  135. #endif
  136.  
  137.  
  138. /*****************************************************************************
  139.   QByteArray stream functions
  140.  *****************************************************************************/
  141.  
  142. QDataStream &operator<<( QDataStream &, const QByteArray & );
  143. QDataStream &operator>>( QDataStream &, QByteArray & );
  144.  
  145.  
  146. /*****************************************************************************
  147.   QString class
  148.  *****************************************************************************/
  149.  
  150. class QRegExp;
  151.  
  152. class QString : public QByteArray        // string class
  153. {
  154. public:
  155.     QString() {}                // make null string
  156.     QString( int size );            // allocate size incl. \0
  157.     QString( const QString &s ) : QByteArray( s ) {}
  158.     QString( const char *str );            // deep copy
  159.     QString( const char *str, uint maxlen );    // deep copy, max length
  160.  
  161.     QString    &operator=( const QString &s );    // shallow copy
  162.     QString    &operator=( const char *str );    // deep copy
  163.  
  164.     bool    isNull()    const;
  165.     bool    isEmpty()    const;
  166.     uint    length()    const;
  167.     bool    resize( uint newlen );
  168.     bool    truncate( uint pos );
  169.     bool    fill( char c, int len = -1 );
  170.  
  171.     QString    copy()    const;
  172.  
  173.     QString    &sprintf( const char *format, ... );
  174.  
  175.     int        find( char c, int index=0, bool cs=TRUE ) const;
  176.     int        find( const char *str, int index=0, bool cs=TRUE ) const;
  177.     int        find( const QRegExp &, int index=0 ) const;
  178.     int        findRev( char c, int index=-1, bool cs=TRUE) const;
  179.     int        findRev( const char *str, int index=-1, bool cs=TRUE) const;
  180.     int        findRev( const QRegExp &, int index=-1 ) const;
  181.     int        contains( char c, bool cs=TRUE ) const;
  182.     int        contains( const char *str, bool cs=TRUE ) const;
  183.     int        contains( const QRegExp & ) const;
  184.  
  185.     QString    left( uint len )  const;
  186.     QString    right( uint len ) const;
  187.     QString    mid( uint index, uint len) const;
  188.  
  189.     QString    leftJustify( uint width, char fill=' ', bool trunc=FALSE)const;
  190.     QString    rightJustify( uint width, char fill=' ',bool trunc=FALSE)const;
  191.  
  192.     QString    lower() const;
  193.     QString    upper() const;
  194.  
  195.     QString    stripWhiteSpace()    const;
  196.     QString    simplifyWhiteSpace()    const;
  197.  
  198.     QString    &insert( uint index, const char * );
  199.     QString    &insert( uint index, char );
  200.     QString    &append( const char * );
  201.     QString    &prepend( const char * );
  202.     QString    &remove( uint index, uint len );
  203.     QString    &replace( uint index, uint len, const char * );
  204.     QString    &replace( const QRegExp &, const char * );
  205.  
  206.     short    toShort( bool *ok=0 )    const;
  207.     ushort    toUShort( bool *ok=0 )    const;
  208.     int        toInt( bool *ok=0 )    const;
  209.     uint    toUInt( bool *ok=0 )    const;
  210.     long    toLong( bool *ok=0 )    const;
  211.     ulong    toULong( bool *ok=0 )    const;
  212.     float    toFloat( bool *ok=0 )    const;
  213.     double    toDouble( bool *ok=0 )    const;
  214.  
  215.     QString    &setStr( const char *s );
  216.     QString    &setNum( short );
  217.     QString    &setNum( ushort );
  218.     QString    &setNum( int );
  219.     QString    &setNum( uint );
  220.     QString    &setNum( long );
  221.     QString    &setNum( ulong );
  222.     QString    &setNum( float, char f='g', int prec=6 );
  223.     QString    &setNum( double, char f='g', int prec=6 );
  224.  
  225.     bool    setExpand( uint index, char c );
  226.  
  227.         operator const char *() const;
  228.     QString    &operator+=( const char *str );
  229.     QString    &operator+=( char c );
  230. };
  231.  
  232.  
  233. /*****************************************************************************
  234.   QString stream functions
  235.  *****************************************************************************/
  236.  
  237. QDataStream &operator<<( QDataStream &, const QString & );
  238. QDataStream &operator>>( QDataStream &, QString & );
  239.  
  240.  
  241. /*****************************************************************************
  242.   QString inline functions
  243.  *****************************************************************************/
  244.  
  245. inline QString &QString::operator=( const QString &s )
  246. { return (QString&)assign( s ); }
  247.  
  248. inline QString &QString::operator=( const char *str )
  249. { return (QString&)duplicate( str, strlen(str)+1 ); }
  250.  
  251. inline bool QString::isNull() const
  252. { return data() == 0; }
  253.  
  254. inline bool QString::isEmpty() const
  255. { return data() == 0 || *data() == '\0'; }
  256.  
  257. inline uint QString::length() const
  258. { return strlen( data() ); }
  259.  
  260. inline bool QString::truncate( uint pos )
  261. { return resize(pos+1); }
  262.  
  263. inline QString QString::copy() const
  264. { return QString( data() ); }
  265.  
  266. inline QString &QString::prepend( const char *s )
  267. { return insert(0,s); }
  268.  
  269. inline QString &QString::append( const char *s )
  270. { return operator+=(s); }
  271.  
  272. inline QString &QString::setNum( short n )
  273. { return setNum((long)n); }
  274.  
  275. inline QString &QString::setNum( ushort n )
  276. { return setNum((ulong)n); }
  277.  
  278. inline QString &QString::setNum( int n )
  279. { return setNum((long)n); }
  280.  
  281. inline QString &QString::setNum( uint n )
  282. { return setNum((ulong)n); }
  283.  
  284. inline QString &QString::setNum( float n, char f, int prec )
  285. { return setNum((double)n,f,prec); }
  286.  
  287. inline QString::operator const char *() const
  288. { return (const char *)data(); }
  289.  
  290.  
  291. /*****************************************************************************
  292.   QString non-member operators
  293.  *****************************************************************************/
  294.  
  295. inline bool operator==( const QString &s1, const QString &s2 )
  296. { return strcmp(s1.data(),s2.data()) == 0; }
  297.  
  298. inline bool operator==( const QString &s1, const char *s2 )
  299. { return strcmp(s1.data(),s2) == 0; }
  300.  
  301. inline bool operator==( const char *s1, const QString &s2 )
  302. { return strcmp(s1,s2.data()) == 0; }
  303.  
  304. inline bool operator!=( const QString &s1, const QString &s2 )
  305. { return strcmp(s1.data(),s2.data()) != 0; }
  306.  
  307. inline bool operator!=( const QString &s1, const char *s2 )
  308. { return strcmp(s1.data(),s2) != 0; }
  309.  
  310. inline bool operator!=( const char *s1, const QString &s2 )
  311. { return strcmp(s1,s2.data()) != 0; }
  312.  
  313. inline bool operator<( const QString &s1, const char *s2 )
  314. { return strcmp(s1.data(),s2) < 0; }
  315.  
  316. inline bool operator<( const char *s1, const QString &s2 )
  317. { return strcmp(s1,s2.data()) < 0; }
  318.  
  319. inline bool operator<=( const QString &s1, const char *s2 )
  320. { return strcmp(s1.data(),s2) <= 0; }
  321.  
  322. inline bool operator<=( const char *s1, const QString &s2 )
  323. { return strcmp(s1,s2.data()) <= 0; }
  324.  
  325. inline bool operator>( const QString &s1, const char *s2 )
  326. { return strcmp(s1.data(),s2) > 0; }
  327.  
  328. inline bool operator>( const char *s1, const QString &s2 )
  329. { return strcmp(s1,s2.data()) > 0; }
  330.  
  331. inline bool operator>=( const QString &s1, const char *s2 )
  332. { return strcmp(s1.data(),s2) >= 0; }
  333.  
  334. inline bool operator>=( const char *s1, const QString &s2 )
  335. { return strcmp(s1,s2.data()) >= 0; }
  336.  
  337. inline QString operator+( const QString &s1, const QString &s2 )
  338. {
  339.     QString tmp( s1.data() );
  340.     tmp += s2;
  341.     return tmp;
  342. }
  343.  
  344. inline QString operator+( const QString &s1, const char *s2 )
  345. {
  346.     QString tmp( s1.data() );
  347.     tmp += s2;
  348.     return tmp;
  349. }
  350.  
  351. inline QString operator+( const char *s1, const QString &s2 )
  352. {
  353.     QString tmp( s1 );
  354.     tmp += s2;
  355.     return tmp;
  356. }
  357.  
  358. inline QString operator+( const QString &s1, char c2 )
  359. {
  360.     QString tmp( s1.data() );
  361.     tmp += c2;
  362.     return tmp;
  363. }
  364.  
  365. inline QString operator+( char c1, const QString &s2 )
  366. {
  367.     QString tmp( c1 );
  368.     tmp += s2;
  369.     return tmp;
  370. }
  371.  
  372.  
  373. #endif // QSTRING_H
  374.